package org.lzy.shop.viewmodel

import android.content.Context
import android.net.Uri
import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import org.lzy.shop.base.BaseViewModel
import org.lzy.shop.datas.FeedBackSubmitRequest
import org.lzy.shop.response.FeedBackItem
import org.lzy.shop.response.FeedbackType
import org.lzy.shop.response.OssUpload
import org.lzy.shop.viewmodel.repository.FeedbackRepository
import org.lzy.shop.viewmodel.repository.lmpl.FeedbackRepositoryImpl

/**
 * 支付的VM层
 */
class FeedBackModel :BaseViewModel() {
    private val feedbackRepository: FeedbackRepository by lazy {
        FeedbackRepositoryImpl()
    }
    val getFeedBackTypeData = MutableLiveData<MutableList<FeedbackType>?>()
    val getFeedBackData = MutableLiveData<MutableList<FeedBackItem>?>()
    val ossUploadLiveData = MutableLiveData<OssUpload?>()
    val uploadImgLiveData = MutableLiveData<String?>()
    val uploadImagesLiveData = MutableLiveData<List<String>?>()
    val getFeedBackSubmitData = MutableLiveData<Any?>()



    /**
     * 获得退货理由
     */
    fun getReasonData(): LiveData<MutableList<FeedbackType>?> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = feedbackRepository.getFeedbackTypes()
                if (response != null) {
                    getFeedBackTypeData.value = response
                } else {
                    getFeedBackTypeData.value = null
                    errorMessage.value = "获取反馈类型失败"
                }
            } catch (e: Exception) {
                e.printStackTrace()
                getFeedBackTypeData.value = null
                errorMessage.value = e.message ?: "获取反馈类型失败"
            }
        }
        return getFeedBackTypeData
    }

    fun getFeedBack(page: Int): LiveData<MutableList<FeedBackItem>?> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = feedbackRepository.getFeedbackList(page)
                if (response != null) {
                    getFeedBackData.value = response
                } else {
                    getFeedBackData.value = null
                    errorMessage.value = "获取反馈列表失败"
                }
            } catch (e: Exception) {
                e.printStackTrace()
                getFeedBackData.value = null
                errorMessage.value = e.message ?: "获取反馈列表失败"
            }
        }
        return getFeedBackData
    }

    /**
     * 获取文件上传凭证
     */
    fun getTencentCredentials(): LiveData<OssUpload?> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = feedbackRepository.getUploadCredentials()
                if (response != null) {
                    ossUploadLiveData.value = response
                } else {
                    ossUploadLiveData.value = null
                    errorMessage.value = "获取上传凭证失败"
                }
            } catch (e: Exception) {
                e.printStackTrace()
                ossUploadLiveData.value = null
                errorMessage.value = e.message ?: "获取上传凭证失败"
            }
        }
        return ossUploadLiveData
    }

    fun uploadImage(
        context: Context,
        imageUri: Uri,
        uploadUrl: String,
        tmpSecretId: String,
        tmpSecretKey: String,
        sessionToken: String
    ): LiveData<String?> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = feedbackRepository.uploadImage(context, imageUri, uploadUrl, tmpSecretId, tmpSecretKey, sessionToken)
                if (response != null) {
                    Log.d("uploadUrl", "文件上传成功，URL: $response")
                    uploadImgLiveData.value = response
                } else {
                    uploadImgLiveData.value = null
                    errorMessage.value = "图片上传失败"
                    Log.d("uploadUrl", "文件上传失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                uploadImgLiveData.value = null
                errorMessage.value = e.message ?: "图片上传异常"
                Log.e("uploadUrl", "文件上传异常: ${e.message}")
            }
        }
        return uploadImgLiveData
    }

    fun uploadImages(
        context: Context,
        imageUris: List<Uri>,
        uploadUrl: String,
        tmpSecretId: String,
        tmpSecretKey: String,
        sessionToken: String
    ): LiveData<List<String>?> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = feedbackRepository.uploadImages(context, imageUris, uploadUrl, tmpSecretId, tmpSecretKey, sessionToken)
                if (response != null) {
                    Log.e("uploadUrl", "批量上传成功，共上传 ${response.size} 个文件")
                    uploadImagesLiveData.value = response
                } else {
                    uploadImagesLiveData.value = null
                    errorMessage.value = "批量图片上传失败"
                    Log.e("uploadUrl", "批量上传失败")
                }
            } catch (e: Exception) {
                e.printStackTrace()
                uploadImagesLiveData.value = null
                errorMessage.value = e.message ?: "批量图片上传异常"
                Log.e("uploadUrl", "批量上传异常: ${e.message}")
            }
        }
        return uploadImagesLiveData
    }

    /**
     * 提交反馈
     */
    fun feedBackSubmit(request: FeedBackSubmitRequest): LiveData<Any?> {
        viewModelScope.launch(Dispatchers.Main) {
            try {
                val response = feedbackRepository.submitFeedback(request)
                if (response != null) {
                    getFeedBackSubmitData.value = response
                } else {
                    getFeedBackSubmitData.value = null
                    errorMessage.value = "提交反馈失败"
                }
            } catch (e: Exception) {
                e.printStackTrace()
                getFeedBackSubmitData.value = null
                errorMessage.value = e.message ?: "提交反馈失败"
            }
        }
        return getFeedBackSubmitData
    }
}